<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/8/5 13:51
 * Modified Time: 2019/8/5 13:51
 * Version: 2.0
 */

namespace App\Modules\MemberCard\Criterias;


use Flower\Core\Repository\Contracts\RepositoryInterface;
use Flower\Core\Repository\Criterias\BaseCriterias;
use function foo\func;

class AppGetMemberCardFlowCriteria extends BaseCriterias
{
    private $member_card_id;

    public function __construct($member_card_id)
    {
        $this->member_card_id = $member_card_id;
    }

    public function apply($model, RepositoryInterface $repository)
    {
        $member_card_id = $this->member_card_id;
        $select = [
            'hx_member_card_flow.id',
            'hx_member_card_flow.card_id',
            'hx_member_card_flow.member_id',
            'hx_member_card_flow.store_id',
            'hx_member_card_flow.type',
            'hx_member_card_flow.card_type',
            'hx_member_card_flow.count',
            'hx_member_card_flow.balance',
            'hx_member_card_flow.user_id',
            'hx_member_card_flow.order_id',
            '(hx_member_card_flow.balance - hx_member_card_flow.real_amount) as present',
            'hx_member_card_flow.real_amount',
            'hx_member_card_flow.created_at'
        ];
        $select = implode($select, ',');

        $model = $model->selectRaw($select)
            ->with(['order' => function ($query) {
                $select = [
                    'hx_order.id',
                    'hx_order.barber_id',
                    'hx_order.store_id',
                    'hx_order.order_no'
                ];
                $select = implode($select, ',');

                $query->selectRaw($select)
                    ->with(['user' => function ($query) {
                        $select = [
                            'hx_user.id',
                            'hx_user.nick'
                        ];
                        $select = implode($select, ',');
                        $query->selectRaw($select);
                    }])->with(['items' => function ($query) {
                        $select = [
                            'hx_order_item.id',
                            'hx_order_item.order_id',
                            'hx_order_item.service_name',
                            'hx_order_item.price',
                            'hx_order_item.count',
                            'hx_order_item.barber_id',
                            'hx_order_item.assistant_id'
                        ];
                        $select = implode($select, ',');
                        $query->selectRaw($select)->with(['barber:id,nick','assistant:id,nick']);
                    }])->with(['store' => function ($query) {
                        $select = [
                            'hx_store.id',
                            'hx_store.name'
                        ];
                        $select = implode($select, ',');
                        $query->selectRaw($select);
                    }]);
            }])
            ->with(['card'=>function ($query){
	            $query->select(['id','type_id'])->with(['cardType']);
            }])
            ->where('card_id',$member_card_id)
            ->orderBy('id','DESC');

        return $model;
    }

}